import scrapy
import re
import json
from bilibili.items import BilibiliItem
import random
import time
class LiSpider(scrapy.Spider):
    name = 'li'

    # allowed_domains = ['bilibili.com']
    # start_urls = ['http://bilibili.com/']
    def start_requests(self):
        name = ['蔡徐坤', '肖战', '王一博', '吴亦凡', '李现']
        name1 = ['蔡徐坤']
        for i in name:
            for x in range(1, 51):
                url = 'https://search.bilibili.com/all?keyword=' + str(i) + '&page=' + str(x)
                time.sleep(random.randint(1,5))
                yield scrapy.Request(url=url, callback=self.yemian)

    def yemian(self, response):

        url = response.xpath('//*[@id="all-list"]/div[1]/div/ul/li/div/div/a/@href').getall()
        # lei = response.xpath('//*[@id="all-list"]/div[1]/ul/li[1]/div/div[1]/span/text()').getall()

        for u in url:
            # print(p,g.replace('\n','').replace(' ',''),t.replace('\n','').replace(' ',''),u)
            # print(name)
            time.sleep(random.randint(1, 5))
            yield scrapy.Request(url='https:' + u, callback=self.test)

    def test(self, response):
        name = response.xpath('//*[@id="viewbox_report"]/h1/span/text()').get()
        time = response.xpath('//*[@id="viewbox_report"]/div/span[3]/text()').get()
        url = response.xpath('/html/head/meta[10]/@content').get()
        up_name = response.xpath('//*[@id="v_upinfo"]/div[2]/div[1]/a[1]/text()').get()
        lei = response.css('span.channel-name::text').getall()
        Url = re.findall('av(.*?)/', url)
        # print(name,time,Url[0],up_name,lei)
        yield scrapy.Request(url='https://api.bilibili.com/x/web-interface/archive/stat?aid=' + str(Url[0]), meta={
            'name': name,
            'time': time,
            'up_name': up_name,
            'lei': "|".join(lei)

        }, callback=self.parse)

    def parse(self, response):
        jd = BilibiliItem()
        bi = json.loads(response.text)
        jd['kan'] = bi['data']['view']  # 播放量
        jd['dan'] = bi['data']['danmaku']  # 弹幕量
        jd['shou'] = bi['data']['favorite']  # 收藏数
        jd['ying'] = bi['data']['coin']  # 硬币数
        jd['fen'] = bi['data']['share']  # 分享数
        jd['zan'] = bi['data']['like']  # 点赞数
        jd['name'] = response.meta['name']
        jd['upname']= response.meta['up_name']
        jd['time']= response.meta['time']
        jd['lei']= response.meta['lei']
        print('***********')
        print(response.meta['lei'])
        yield jd
